package com.org.manhu.chargehelper.ui;

import java.util.Timer;
import java.util.TimerTask;

import com.org.manhu.chargehelper.ChargeApplication;
import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.service.ServerService;
import com.org.manhu.chargehelper.tools.AppManager;
import com.org.manhu.chargehelper.tools.NetUtils;
import com.org.manhu.chargehelper.tools.ToastUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

/**
 * @文件名：SplashActivity.java
 * @包名：com.org.manhu.chargehelper.ui
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2015年2月5日 上午10:54:27
 *
 * @描述：
 */
public class WelcomeActivity extends Activity {

	private Timer timer = new Timer();
	private TimerTask task;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcom);
		AppManager.getAppManager().addActivity(this);

		ChargeApplication.isBoxConnected = false;

		task = new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				openHomePage();
			}

		};

		if (!ChargeApplication.isBoxConnected) {
			checkNetAndOpenSetting();
		}

		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(connectionReceiver, intentFilter);
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 50) {
				handler.removeMessages(50);
				openHomePage();
			}
		};
	};

	private BroadcastReceiver connectionReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
			NetworkInfo mobNetInfo = connectMgr
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo wifiNetInfo = connectMgr
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
				// unconnect network
			} else {
				// connect network
				// timer.schedule(task, 2000);
				ToastUtils.showShort(WelcomeActivity.this, "网络已连接");
				handler.sendEmptyMessageDelayed(50, 3000);
			}

		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(connectionReceiver);
	}

	/**
	 * @描述： 检查网络并代开设置界面
	 */
	private void checkNetAndOpenSetting() {
		if (!NetUtils.isConnected(WelcomeActivity.this)) {
			AlertDialog.Builder ab = new Builder(WelcomeActivity.this);
			ab.setTitle("开启网络服务");
			ab.setMessage("本软件需要使用网络资源，是否开启网络？");
			ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					NetUtils.openSetting(WelcomeActivity.this);
				}
			});
			ab.setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			ab.show();

		} else {
			timer.schedule(task, 5000);
		}
	}

	/**
	 * @描述：
	 */
	private void openHomePage() {
		Intent intent = new Intent(WelcomeActivity.this, HomeActivity.class);
		startActivity(intent);
		finish();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		AppManager.getAppManager().AppExit(this);
		timer.cancel();
		handler.removeMessages(50);
	}
}
